package com.olc.railing.services;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.google.gson.Gson;
import com.olc.railing.base.BaseApplication;
import com.olc.railing.busniess.DataBusiness;
import com.olc.railing.busniess.MyThreadPoolExecutor;
import com.olc.railing.model.BillInfo;
import com.olc.railing.orm.BillInfoDao;
import com.olc.railing.utils.MyLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class UploadTradeInfoServices extends Service {
    private static final String TAG = "UploadTradeInfoServices";
    // Binder given to clients
    private final IBinder binder = new UploadBinder();
    public static final int UPLOAD_ERROR_COUNT = 2;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){

                case DataBusiness.TRADE_UPLOAD_FAIL:
                    String uuid1 = (String) msg.obj;
                    MyLog.d(MyLog.TAG,"uuid1 = "+uuid1);
                    BillInfo billInfo1 = new BillInfoDao(BaseApplication.getApplication()).queryByOrderNo(uuid1);
                    int errorCount =  billInfo1.getErrorCount();
                    errorCount++;
                    MyLog.d(MyLog.TAG,"errorCount = "+errorCount);
                    handler.removeMessages(0);
                    if(errorCount>UPLOAD_ERROR_COUNT){
                        billInfo1.setUploadStatus(1);
                        billInfo1.setErrorCount(errorCount);
                        new BillInfoDao(BaseApplication.getApplication()).update(billInfo1);
                    }else{
                        uploadTradeInfo(uuid1);
                    }
                    break;
                case DataBusiness.TRADE_UPLOAD_SUCCESS:
                    String uuid = (String) msg.obj;
                 //   MyLog.d(MyLog.TAG,"TRADE_UPLOAD_SUCCESS uuid = "+uuid);
                    BillInfo billInfo = new BillInfoDao(BaseApplication.getApplication()).queryByOrderNo(uuid);
                    if(billInfo!=null){
                        MyLog.d(MyLog.TAG,"TRADE_UPLOAD_SUCCESS UploadStatus = "+billInfo.getUploadStatus());
                        billInfo.setUploadStatus(1);
                        new BillInfoDao(BaseApplication.getApplication()).update(billInfo);
                    }else{
                        MyLog.d(MyLog.TAG,"TRADE_UPLOAD_SUCCESS billInfo is null ");
                    }
                    break;
            }
        }
    };



    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class UploadBinder extends Binder {
        public  UploadTradeInfoServices getService() {
            // Return this instance of LocalService so clients can call public methods
            return UploadTradeInfoServices.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        MyLog.d(MyLog.TAG,TAG+" onBind");
        return binder;
    }

    public void uploadTradeInfo(String uuid){
        MyLog.e(MyLog.TAG,"uploadTradeInfo");
        MyThreadPoolExecutor.getMyThreadPoolInstance().getMyThreadPoolExecutor().execute(new Runnable() {
            @Override
            public void run() {
                BillInfo billInfo = new BillInfoDao(BaseApplication.getApplication()).queryByOrderNo(uuid);
                MyLog.d(MyLog.TAG,"BillInfo = "+billInfo.toString());
                DataBusiness.getInstance(BaseApplication.getApplication()).uploadTradeInfo(BaseApplication.getApplication(),billInfo,handler);
            }
        });

        //对于上传成功的数据，本地数据库定时清理
    }

}
